package com.fingard.dsp.bank.directbank.hbc11;

import com.fingard.dsp.bank.directConfig.FtpSet;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.FtpReceiver;
import com.fingard.net.SftpPortClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author XueJy on 2018/9/19.
 * @version 1.0
 */
public class HBC11Base extends DirectBase {
    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    /**
     * 字符串向左填充指定位数空格
     *
     * @param sourceStr
     * @param strLength
     * @return
     * @throws Exception
     */
    public String leftFill(String sourceStr, int strLength) throws Exception {
        int length = 0;
        try {
            if (null != sourceStr) {
                length = sourceStr.getBytes(getCharset()).length;
            }
            for (int i = 0; i < strLength - length; i++) {
                if (sourceStr == null)
                    sourceStr = " ";
                else
                    sourceStr = " " + sourceStr;
            }
        } catch (Exception e) {
            throw (e);
        }
        return sourceStr;
    }

    /**
     * 字符串空格去除
     *
     * @param sourceStr
     * @return
     */
    public String delBlank(String sourceStr) {
        return sourceStr.replaceAll(" ", "");
    }
    protected boolean uploadFile(String fullName) throws IOException {
        FtpSet ftpSet = bankFront.getFtpSet("bat");
        String upDir = ftpSet.upDir;
        File file = new File(fullName);
        String filename = upDir + File.separator + file.getName();
        filename = filename.replaceAll("\\\\", "/");
        FileInputStream inputStream = null;
        SftpPortClient sftpPortClient = null;
        try {
            sftpPortClient = new SftpPortClient(ftpSet);
            sftpPortClient.connectBySSH();
            inputStream = new FileInputStream(file);
//            ChannelSftp channelSftp = (ChannelSftp) jschMap.get("channel");
//            channelSftp.put(inputStream,filename);
            FtpReceiver receiver = sftpPortClient.uploadFile("", inputStream, filename);
            if (receiver.isCompleted) {
                WriteBankLogLn2("上传文件：" + filename + "成功");
            } else {
                WriteBankLogLn2("上传文件：" + filename + "失败");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            WriteBankLogLn("上传文件失败", e);
            return false;
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            sftpPortClient.disconnect();
        }
        return true;
    }
}
